package com.zq.learn.akka.classic;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.event.Logging;
import akka.event.LoggingAdapter;

/**
 * CGreeter
 *
 * @author ZhangQi
 * @date 2020/3/13
 */
public class CGreeter extends AbstractActor {
  private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
  //响应Actor
  private final ActorRef replayTo ;

  public CGreeter(ActorRef replayTo) {
    this.replayTo = replayTo;
  }

  /**
   * 用于实例化Actor
   * @param greeter
   * @return
   */
  public static Props props(ActorRef greeter) {
    return Props.create(CGreeter.class,()->new CGreeter(greeter));
  }

  /**
   * 问候消息
   */
  public static final class CGreet{
    public final String whom;

    public CGreet(String whom) {
      this.whom = whom;
    }
  }


  /**
   * 接收方法（重写）
   *
   * @return
   */
  @Override public Receive createReceive() {
    return receiveBuilder().match(CGreet.class,greet->{
      log.info("Hello {}!",greet.whom);
      replayTo.tell(new CGreeterBot.CGreeted(greet.whom),getSelf());
    }).build();
  }
}
